<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-product-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">商品名称：</label>
                <div class="col-sm-8">
                    <input id="name" name="name" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">商品单价：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" class="form-control" placeholder="yyyy-MM-dd HH:mm:ss" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">库存数量：</label>
                <div class="col-sm-8">
                    <input name="count" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">商品描述：</label>
                <div class="col-sm-8">
                    <textarea name="describe" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">商品图片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="img">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="img" name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">

        jQuery(function() {
            jQuery.validator.methods.compareDate = function (value, element, param) {
                //var startDate = jQuery(param).val() + ":00";补全yyyy-MM-dd HH:mm:ss格式
                //value = value + ":00";
                var startDate = jQuery(param).val();
                var date1 = new Date(Date.parse(startDate.replace("-", "/")));
                var date2 = new Date(Date.parse(value.replace("-", "/")));
                return date1 < date2;
            }
        });

        var prefix = ctx + "system/product"


        jQuery.validator.methods.compareDate = function(value, element, param) {
            var startDate = jQuery(param).val() + ":00";//补全yyyy-MM-dd HH:mm:ss格式
            value = value + ":00";
            var startDate = jQuery(param).val();

            var date1 = new Date(Date.parse(startDate.replace("-", "/")));
            var date2 = new Date(Date.parse(value.replace("-", "/")));
            return date1 < date2;
        };

        $("#form-product-add").validate({
            focusCleanup: true,
            rules: {
                name :{
                    required: true,
                    remote:{
                        url : "/system/product/checkproductname",
                        type : "post",
                        dataType: "json",
                        data: {
                            name: function(){
                                return $("#name").val();
                            }
                        },
                        dataFilter: function(data, type) {
                            return $.validate.unique(data);
                        }
                    }

                },
                price:{
                    required: true,
                    number: true,

                },
                startTime: {
                    required: true,
                },
                endTime: {
                    required: true,
                    compareDate : "#startTime"
                },
                count:{
                    required:true,
                    digits:true,
                }
            },
            messages:{
                name:{
                    required:"请输入商品名称",
                    remote: "此商品名称已经存在"
                },
                price: {
                    required: "请输入商品单价"
                },
                count: {
                    required: "请输入商品库存"
                },
                startTime: {
                    required: "请输入开始时间"
                },
                endTime: {
                    required: "请输入结束时间",
                    compareDate: "结束需大于开始时间"
                },
                describe: {
                    required: "请输入商品描述"
                }
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-product-add').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            minView: 'hour',
            minuteStep:1,
            autoclose:true //选择日期后自动关闭
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:00", //选择日期后，文本框显示的日期格式
            language: 'zh-CN', //汉化
            minView: 'hour',
            minuteStep:1,
            autoclose:true //选择日期后自动关闭
        });

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })

        $.validator.addMethod("digits",function(value,element,params){
            var digits= /^[1-9]\d*$/;
            return this.optional(element)||(digits.test(value));
        },"请输入正整数！");

        $.validator.addMethod("number",function(value,element,params){
            var number= /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;
            return this.optional(element)||(number.test(value));
        },"请输入规范的商品价格！");

    </script>

</body>
</html>